package com.movitech.mobile.cz.base.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.license.LicenseUtil;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.DateUtils;
import com.movitech.mobile.cz.base.common.util.WebUtils;
import com.movitech.mobile.cz.base.purchase.entity.WFlowFormSupplierEntry;
import com.movitech.mobile.cz.base.purchase.entity.ext.ExtWFlowFormSupplierEntry;
import com.movitech.mobile.cz.base.purchase.mapper.WFlowFormSupplierEntryMapper;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtWFlowFormSupplierEntryMapper;
import com.movitech.mobile.cz.base.purchase.service.WFlowFormSupplierEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 流程表单-供应商入围 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-15
 */
@Service
public class WFlowFormSupplierEntryServiceImpl extends ServiceImpl<WFlowFormSupplierEntryMapper, WFlowFormSupplierEntry> implements WFlowFormSupplierEntryService {

	@Autowired
	private ExtWFlowFormSupplierEntryMapper extWFlowFormSupplierEntryMapper;

	@Override
	public List<ExtWFlowFormSupplierEntry> getFlowFormSupplierEntryList(Map<String, Object> param) {
		return extWFlowFormSupplierEntryMapper.getFlowFormSupplierEntryList(param);
	}

	@Override
	public List<WFlowFormSupplierEntry> getByFormId(String formId) {
		LicenseUtil.INSTANCE.validate();
		return selectList(new EntityWrapper<WFlowFormSupplierEntry>().eq("flow_form_id",formId));
	}

	@Override
	public List<WFlowFormSupplierEntry> getReplyAgreeByFormId(String formId) {
		LicenseUtil.INSTANCE.validate();
		return selectList(new EntityWrapper<WFlowFormSupplierEntry>().eq("flow_form_id",formId)
						.eq("reply_situation", CommonConstants.SUPPLIER_RESPONSE_LETTER_YES));
	}

	@Override
	public boolean updateReturnStatusByFormIdAndSupplierId(String formId, String supplierId ,int status) {
		WFlowFormSupplierEntry wFlowFormSupplierEntry = new WFlowFormSupplierEntry();
		wFlowFormSupplierEntry.setReplySituation(status);
		wFlowFormSupplierEntry.setReplyTime(DateUtils.getNow());
		wFlowFormSupplierEntry.setReplyIp(WebUtils.getIpAddr());
		return update(wFlowFormSupplierEntry,new EntityWrapper<WFlowFormSupplierEntry>().
				eq("flow_form_id",formId).eq("supplier_id",supplierId));
	}

	@Override
	public List<WFlowFormSupplierEntry> getReplyAgreeByFormIdGroupBySupplier(String formId) {
		LicenseUtil.INSTANCE.validate();
		return selectList(new EntityWrapper<WFlowFormSupplierEntry>().eq("flow_form_id",formId)
				.eq("reply_situation", CommonConstants.SUPPLIER_RESPONSE_LETTER_YES).groupBy("supplier_id"));
	}

	@Override
	public List<String> getSupplierIdsWithAgree(String formId) {
		LicenseUtil.INSTANCE.validate();
		return extWFlowFormSupplierEntryMapper.getSupplierIdsWithAgree(formId);
	}


}
